<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      titleFontSize: 0,
      startValue: 0,
      endValue: 9,
      timerId: null,
    };
  },
  // created() {
  //   this.$socket.registerCallBack("rankData", this.getData);
  // },
  mounted() {
    this.initChart();
    // this.$socket.send({
    //   action: "getData",
    //   socketType: "rankData",
    //   chartName: "rank",
    //   value: "",
    // })
    this.getData();
    window.addEventListener("resize", this.screenAdpter);
    this.screenAdpter();
  },
  distoryed() {
    window.removeEeventListener("resize", this.screenAdpter);
    clearInterval(this.timerId);
    // this.$socket.UnRegisterCallBack("rankData");
  },
  computed: {},
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, "chalk");
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLable: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
            bottom: "20%"
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      const { data: ret } = await this.$http("rank");
      this.allData = ret;
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updataChart();
      this.startInterval();
    },
    updataChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      // 所有省份的数组
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      // 所有省份对应的销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue, // 区域缩放的起点值
          endValue: this.endValue, // 区域缩放的终点值
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0],
                    },
                    {
                      offset: 1,
                      color: targetColorArr[1],
                    },
                  ]);
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0],
                    },
                    {
                      offset: 1,
                      color: targetColorArr[1],
                    },
                  ]);
                } else {
                  targetColorArr = colorArr[2];
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0],
                    },
                    {
                      offset: 1,
                      color: targetColorArr[1],
                    },
                  ]);
                }
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdpter() {
      this.titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const adpterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        series: [
          {
            barWidth: this.titleFontSize,
            itemStyle: {
              barBorderRadius: [this.titleFontSize / 2, this.titleFontSize / 2],
            },
          },
        ],
      };
      this.chartInstance.setOption(adpterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updataChart();
      }, 2000);
    },
  },
};
</script>
